/**********************************************************************************************
Step 0: Basic setup
**********************************************************************************************/
** settings

* Choose whether to focus on urban areas or not
local use_urban = 0
*local use_urban = 1

clear all
set scheme s1mono

use "$DATA_OUT/NMDB_ASMB_respondents", clear

* Drop defaulters that were never current with 4 years of the survey
drop if defaulter==1 & mi(last_current_month_b4_default)

* Drop foreclosures not in the sample of defaulters
drop if foreclosure==1 & in_default_sample==0  
count


gen one = 1
gen urban = inlist(rucc,"01","02") //see https://www.ers.usda.gov/data-products/rural-urban-continuum-codes/.  1 or 2 includes only counties in metro areas with 250,000+ residents.  



if `use_urban' == 1 {
	keep if urban==1
	local urban_title = "_urban"
}

svyset [pweight = analysis_weight]

gen LTV_FHFA_st_capped = min(LTV_FHFA_st,120)
gen LTV_FHFA_st_bin = round(LTV_FHFA_st_capped,20) // e.g. the "80" bin includes LTVs from 70-90
replace LTV_FHFA_st_bin = 20 if LTV_FHFA_st_bin==0 // put 0-10 in the same bin as 10-30


gen LTV_FHFA_cnty_capped = min(LTV_FHFA_county,120)
gen LTV_FHFA_cnty_bin = round(LTV_FHFA_cnty_capped,20)
replace LTV_FHFA_cnty_bin = 20 if LTV_FHFA_cnty_bin==0

gen LTV_FHFA_tr_capped = min(LTV_FHFA_tr,120)
gen LTV_FHFA_tr_bin = round(LTV_FHFA_tr_capped,20)
replace LTV_FHFA_tr_bin = 20 if LTV_FHFA_tr_bin==0

* Account properly for missing LTVs.  
replace LTV_FHFA_st_bin   = . if mi(LTV_FHFA_st) 
replace LTV_FHFA_cnty_bin = . if mi(LTV_FHFA_county) 
replace LTV_FHFA_tr_bin   = . if mi(LTV_FHFA_tract) 




********************************************************************************
** Now look at foreclosure rates etc by LTV
********************************************************************************


preserve
collapse (mean) foreclosure [pweight = analysis_weight], by(LTV_FHFA_st_bin)
rename LTV_FHFA_st_bin LTV_bin
rename foreclosure foreclosure_st
save "$DATA_OUT/processing/foreclosure_rates_by_LTV_st_bin", replace
restore

preserve
collapse (mean) foreclosure [pweight = analysis_weight], by(LTV_FHFA_cnty_bin)
rename LTV_FHFA_cnty_bin LTV_bin
rename foreclosure foreclosure_cnty
save "$DATA_OUT/processing/foreclosure_rates_by_LTV_cnty_bin", replace
restore

collapse (mean) foreclosure [pweight = analysis_weight], by(LTV_FHFA_tr_bin)
rename foreclosure foreclosure_tr
rename LTV_FHFA_tr_bin LTV_bin
merge 1:1 LTV_bin using "$DATA_OUT/processing/foreclosure_rates_by_LTV_st_bin", assert(match) nogen
merge 1:1 LTV_bin using "$DATA_OUT/processing/foreclosure_rates_by_LTV_cnty_bin", assert(match) nogen

* Normalize foreclosure rates
foreach var in foreclosure_st foreclosure_cnty foreclosure_tr {
	gen `var'_80 = `var' if LTV_bin==80
	egen max_`var'_80 = max(`var'_80)
	replace `var' = `var' / max_`var'_80
	
	drop `var'_80 max_`var'_80
}


label define ltvlabs 20 "0-30" 40 "30-50" 60 "50-70" 80 "70-90" 100 "90-110" 120 "110+" 
label values LTV_bin ltvlabs

graph bar foreclosure_st foreclosure_cnty foreclosure_tr , over(LTV_bin) b1title("LTV Bin") ytitle("Normalized Foreclosure Rate") ysc(range(0 8)) ytick(0(1)8) ylabel(0(1)8) legend(label(1 "State") label(2 "County") label(3 "Tract") label(4 "FGW") rows(1)) graphregion(color(white)) bgcolor(white)

graph export "$OUTPUT/Foreclosure_risk_by_LTV`urban_title'.pdf", replace





